package com.itheima;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Test001 {
    public static void main(String[] args) {
        /*
        有以下double数组：
	    	double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
	        请编程计算它们的总值及平均值(四舍五入保留小数点后2位)
         */
        double[] array = {0.1,0.2,2.1,3.2,5.56,7.21};
        //先创建一个sum对象;
        BigDecimal sum = new BigDecimal("0");
        //遍历集合,让每一个元素相加赋值给sum;
        for (int i = 0; i < array.length; i++) {
            sum=sum.add(new BigDecimal(array[i]+""));
        }
        System.out.println("总值为"+sum);
        System.out.println("平均值为:"+sum.divide(new BigDecimal(array.length),3,RoundingMode.HALF_UP));


    }

    private static void method01(double[] array) {
        BigDecimal sum = new BigDecimal("0");
        for (int i = 0; i < array.length; i++) {
            sum = sum.add(new BigDecimal(array[i]+""));
        }
        System.out.println(sum);
        System.out.println(sum.divide(new BigDecimal("5"),2, RoundingMode.HALF_UP));
    }

    private static void method() {
        double[] array = {0.1, 0.2, 2.1, 3.2, 5.56, 7.21};
        //定义一个sum变量;
        BigDecimal sum = new BigDecimal("0");
        for (int i = 0; i < array.length; i++) {
            sum= sum.add(new BigDecimal(array[i]+""));
        }
        System.out.println(sum);
        System.out.println(sum.divide(new BigDecimal(array.length),2, RoundingMode.HALF_UP));
    }
}
